const Mock = require('mockjs')

Mock.Random.extend({
  myProm: function () {
    let myColors = ['商品赠券', '赠送积分', '下单立减', '赠送礼品',
      '分期免息', '换新无忧', '限时秒杀', '无忧退换', '现货速发']
    let colorList = getArrRandomly(myColors)
    return colorList.slice(0, 2)
  }
})

getArrRandomly = (arr) => {
  let len = arr.length
  for (let i = len - 1; i >= 0; i--) {
    let randomIndex = Math.floor(Math.random() * (i + 1))
    let itemIndex = arr[randomIndex]
    arr[randomIndex] = arr[i]
    arr[i] = itemIndex
  }
  return arr
}

exports.getShopDetail = ctx => {
  let temp = Mock.mock({
    sale: '@integer(100,1000)',
    colorImgSrc: ''
  })


  let { id } = ctx.query

  id = Number(id)

  if (id === 233) {
    ctx.body = {
      meta: {
        code: 200
      },
      body: {
        id: 233,
        name: '荣耀30',
        model: ['Pro 8GB+128GB', 'Pro 8GB+256GB', 'Pro+ 8GB+128GB', 'Pro+ 12GB+256GB'],
        bundlePackage: ['官方标配', '套餐一', '套餐二', '套餐三'],
        characteristic: '50倍超稳远摄 超感光高清夜拍 双模5G 麒麟990 5G SoC',
        subTitle: '【618返场 现货速发】①赠运动蓝牙耳机+魔方音箱②享24期免息③整点购机赠好礼④晒单限量赠官方保护壳⑤赠网易云手机摄影课程⑥下单抽5000元旅游基金',
        feature: ['分期免息'],
        prom: [
          {
            name: '赠品', content: [
              { id: 7788, name: '荣耀xSport 运动蓝牙耳机（极光蓝）极光蓝', imgSrc: '/images/product/gift-1.png' },
              { id: 7789, name: '荣耀魔方蓝牙音箱（树莓红）树莓红', imgSrc: '/images/product/gift-2.png' },
            ]
          },
          { name: '下单立减', content: '下单立减300元', price: 300 },
          { name: '一站式换新', content: 'APP专享，最高补贴400元' },
          { name: '限量送', content: '10/12/14/16/20点下单前200限量送荣耀手环4 Running版（荣耀指定手机共享）' }
        ],
        product: [
          {
            color: '幻夜黑',
            colorId: 1,
            colorSrc: '/images/product/Honor30Pro-color-1.png',
            picSrc: [
              '/images/product/Honor30Pro-color-1-1.png',
              '/images/product/Honor30Pro-color-1-2.png',
              '/images/product/Honor30Pro-color-1-3.png',
              '/images/product/Honor30Pro-color-1-4.png',
              '/images/product/Honor30Pro-color-1-5.png',
              '/images/product/Honor30Pro-color-1-6.png',
              '/images/product/Honor30Pro-color-1-7.png',
            ],
            bundlePackage: [
              { packageID: 1, package_name: '官方标配', price: 0, able: true },
              { packageID: 2, package_name: '套餐一', price: 100, able: true },
              { packageID: 3, package_name: '套餐二', price: 200, able: true },
              { packageID: 4, package_name: '套餐三', price: 300, able: true },
            ],
            model: [
              { modelId: 1, version: 'Pro', memory: '8GB+128GB', price: 3999.00, able: true },
              { modelId: 2, version: 'Pro', memory: '8GB+256GB', price: 4399.00, able: true },
              { modelId: 3, version: 'Pro+', memory: '8GB+128GB', price: 4999.00, able: true },
              { modelId: 4, version: 'Pro+', memory: '12GB+256GB', price: 5499.00, able: true },
            ],
          },
          {
            color: '绿野仙踪',
            colorId: 2,
            colorSrc: '/images/product/Honor30Pro-color-2.png',
            picSrc: [
              '/images/product/Honor30Pro-color-2-1.png',
              '/images/product/Honor30Pro-color-2-2.png',
              '/images/product/Honor30Pro-color-2-3.png',
              '/images/product/Honor30Pro-color-2-4.png',
              '/images/product/Honor30Pro-color-2-5.png',
              '/images/product/Honor30Pro-color-2-6.png',
              '/images/product/Honor30Pro-color-2-7.png',
            ],
            bundlePackage: [
              { packageID: 1, package_name: '官方标配', price: 0, able: true },
              { packageID: 2, package_name: '套餐一', price: 100, able: true },
              { packageID: 3, package_name: '套餐二', price: 200, able: true },
              { packageID: 4, package_name: '套餐三', price: 300, able: false },
            ],
            model: [
              { modelId: 1, version: 'Pro', memory: '8GB+128GB', price: 3999.00, able: true },
              { modelId: 2, version: 'Pro', memory: '8GB+256GB', price: 4399.00, able: true },
              { modelId: 3, version: 'Pro+', memory: '8GB+128GB', price: 4999.00, able: true },
              { modelId: 4, version: 'Pro+', memory: '12GB+256GB', price: 5499.00, able: false },
            ]
          },
          {
            color: '钛空银',
            colorId: 3,
            colorSrc: '/images/product/Honor30Pro-color-3.png',
            picSrc: [
              '/images/product/Honor30Pro-color-3-1.png',
              '/images/product/Honor30Pro-color-3-2.png',
              '/images/product/Honor30Pro-color-3-3.png',
              '/images/product/Honor30Pro-color-3-4.png',
              '/images/product/Honor30Pro-color-3-5.png',
              '/images/product/Honor30Pro-color-3-6.png',
              '/images/product/Honor30Pro-color-3-7.png',
            ],
            bundlePackage: [
              { packageID: 1, package_name: '官方标配', price: 0, able: true },
              { packageID: 2, package_name: '套餐一', price: 100, able: true },
              { packageID: 3, package_name: '套餐二', price: 200, able: true },
              { packageID: 4, package_name: '套餐三', price: 300, able: true },],
            model: [
              { modelId: 1, version: 'Pro', memory: '8GB+128GB', price: 3999.00, able: true },
              { modelId: 2, version: 'Pro', memory: '8GB+256GB', price: 4399.00, able: true },
              { modelId: 3, version: 'Pro+', memory: '8GB+128GB', price: 4999.00, able: true },
              { modelId: 4, version: 'Pro+', memory: '12GB+256GB', price: 5499.00, able: true },
            ]
          },
          {
            color: '霓虹紫',
            colorId: 4,
            colorSrc: '/images/product/Honor30Pro-color-4.png',
            picSrc: [
              '/images/product/Honor30Pro-color-4-1.png',
              '/images/product/Honor30Pro-color-4-2.png',
              '/images/product/Honor30Pro-color-4-3.png',
              '/images/product/Honor30Pro-color-4-4.png',
              '/images/product/Honor30Pro-color-4-5.png',
              '/images/product/Honor30Pro-color-4-6.png',
              '/images/product/Honor30Pro-color-4-7.png',
            ],
            bundlePackage: [
              { packageID: 1, package_name: '官方标配', price: 0, able: true },
              { packageID: 2, package_name: '套餐一', price: 100, able: true },
              { packageID: 3, package_name: '套餐二', price: 200, able: false },
              { packageID: 4, package_name: '套餐三', price: 300, able: false },
            ],
            model: [
              { modelId: 1, version: 'Pro', memory: '8GB+128GB', price: 3999.00, able: true },
              { modelId: 2, version: 'Pro', memory: '8GB+256GB', price: 4399.00, able: true },
              { modelId: 3, version: 'Pro+', memory: '8GB+128GB', price: 4999.00, able: false },
              { modelId: 4, version: 'Pro+', memory: '12GB+256GB', price: 5499.00, able: false },
            ]
          },
          {
            color: '流光幻镜',
            colorId: 5,
            colorSrc: '/images/product/Honor30Pro-color-5.png',
            picSrc: [
              '/images/product/Honor30Pro-color-5-1.png',
              '/images/product/Honor30Pro-color-5-2.png',
              '/images/product/Honor30Pro-color-5-3.png',
              '/images/product/Honor30Pro-color-5-4.png',
              '/images/product/Honor30Pro-color-5-5.png',
              '/images/product/Honor30Pro-color-5-6.png',
              '/images/product/Honor30Pro-color-5-7.png',
            ],
            bundlePackage: [
              { packageID: 1, package_name: '官方标配', price: 0, able: true },
              { packageID: 2, package_name: '套餐一', price: 100, able: true },
              { packageID: 3, package_name: '套餐二', price: 200, able: false },
              { packageID: 4, package_name: '套餐三', price: 300, able: true },],
            model: [
              { modelId: 1, version: 'Pro', memory: '8GB+128GB', price: 3999.00, able: true },
              { modelId: 2, version: 'Pro', memory: '8GB+256GB', price: 4399.00, able: true },
              { modelId: 3, version: 'Pro+', memory: '8GB+128GB', price: 4999.00, able: false },
              { modelId: 4, version: 'Pro+', memory: '12GB+256GB', price: 5499.00, able: false },
            ]
          },
        ]
      }
    }
  } else {
    let data = Mock.mock({
      meta: {
        code: 200
      },
      body: {
        id: id,
        name: '@ctitle(3,5)',
        model: ['Pro 8GB+128GB', 'Pro 8GB+256GB', 'Pro+ 8GB+128GB', 'Pro+ 12GB+256GB'],
        bundlePackage: ['官方标配', '套餐一', '套餐二', '套餐三'],
        characteristic: '@ctitle(20,30)',
        subTitle: '@ctitle(60,80)',
        feature: ['分期免息'],
        'prom|1': [
          [
            { name: '下单立减', content: `下单立减${ temp.sale }元`, price: temp.sale },
            { name: '一站式换新', content: 'APP专享，最高补贴400元' },
            { name: '限量送', content: '10/12/14/16/20点下单前200限量送荣耀手环4 Running版（荣耀指定手机共享）' }
          ],
          [
            {
              name: '赠品', content: [
                { id: '@integer(100,1000)', name: '@title(5,10)', imgSrc: '@image(55x55,@color,@word)' },
                { id: '@integer(100,1000)', name: '@title(5,10)', imgSrc: '@image(55x55,@color,@word)' },
              ]
            },
            { name: '一站式换新', content: 'APP专享，最高补贴400元' },
            { name: '限量送', content: '10/12/14/16/20点下单前200限量送荣耀手环4 Running版（荣耀指定手机共享）' }
          ]
        ],
        'product|3-5': [
          {
            color: '@ctitle(2,4)',
            colorId: '@integer(0,9)',
            colorSrc: '@image(55x55,@color,@word)',
            'picSrc|5-7': ['@image(800x800,@color,@word)'],
            bundlePackage: [
              { packageID: 1, package_name: '官方标配', price: 0, able: true },
              { packageID: 2, package_name: '套餐一', price: 100, able: true },
              { packageID: 3, package_name: '套餐二', price: 200, able: true },
              { packageID: 4, package_name: '套餐三', price: 300, able: true },
            ],
            model: [
              { modelId: 1, version: 'Pro', memory: '8GB+128GB', price: '@integer(1000,6000)', able: true },
              { modelId: 2, version: 'Pro', memory: '8GB+256GB', price: '@integer(1000,6000)', able: true },
              { modelId: 3, version: 'Pro+', memory: '8GB+128GB', price: '@integer(1000,6000)', able: true },
              { modelId: 4, version: 'Pro+', memory: '12GB+256GB', price: '@integer(1000,6000)', able: true },
            ],
          },
        ]
      }
    })

    ctx.body = data
  }

}

exports.getShopList = ctx => {
  let data = Mock.mock({
    'list|6-15':
      [{
        id: '@integer(100,1000)',
        name: '@title(3,5)',
        price: '@integer(1000,8000)',
        comment: '@integer(1000,11000)',
        rate: '@integer(96,99)',
        imgSrc: '@image(200x200,@color,@word)',
        update: '@date',
        prom: '@myProm'
      }]
  })

  ctx.body = {
    meta: {
      code: 200
    },
    body: data
  }
}
